home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / scopedisk24 / Qrt14src / lexer.c < prev    next >
C/C++ Source or Header  |  1988-08-23  |  5KB  |  254 lines

  1.  
  2. /**********************************************************
  3.  
  4.       Small lexical analyser for inout. Also contains
  5.       some bounds checking code.
  6.  
  7.  **********************************************************/
  8.  
  9.  
  10. #include "qrt.h"
  11.  
  12. extern int linenumber;
  13. char *malloc();
  14. float atof();
  15.  
  16.  
  17. /**********************************************************
  18.  
  19.            Transforms all white spaces to blanks
  20.  
  21.  **********************************************************/
  22.  
  23. char towhite(c) char c; {
  24.   if (c=='\n') linenumber++;
  25.  
  26.   if ((c=='\n') ||
  27.       (c=='=')  ||
  28.       (c==',')  ||
  29.       (c==';'))     return(' ');
  30.  
  31.   return(c);
  32. }
  33.  
  34.  
  35. /**********************************************************
  36.  
  37.         Removes blank space before next token
  38.  
  39.  **********************************************************/
  40.  
  41. rmspace() {
  42.   char c;
  43.   while (((c=towhite(fgetc(stdin)))==' ') && !feof(stdin));
  44.   ungetc(c,stdin);
  45. }
  46.  
  47. /**********************************************************
  48.  
  49.  Comment Killer - Added 16 Jun 88 to handle nested comments
  50.  
  51.  **********************************************************/
  52.  
  53. Comment_Killer()
  54. {
  55.   char c;
  56.  
  57.   c='\0';
  58.   while (c != '}' && !feof(stdin)) {
  59.     c = towhite(fgetc(stdin));
  60.     if (c == '{') Comment_Killer();
  61.   }
  62. }
  63.  
  64. /**********************************************************
  65.  
  66.                Get next token from stdin
  67.  
  68.  **********************************************************/
  69.  
  70. GetToken(s)                          /* get a token from stdio */
  71.   char s[];
  72. {                                    /* */
  73.   char c; int x;
  74.  
  75.   x=0; s[0]=c='\0';                       /* char count */
  76.  
  77.   rmspace();
  78.  
  79.   while (!feof(stdin) && x<(SLEN-1)) {
  80.     s[x++]=c=toupper(towhite(fgetc(stdin)));
  81.     if (c==' ') { s[--x]='\0'; break; }
  82.     if (c=='(' || c==')') {
  83.       if (x==1) { s[x]='\0'; break; }
  84.       else {
  85.         ungetc(c,stdin); s[--x]='\0'; break;
  86.       }
  87.     }
  88.     if (c=='{') {
  89.       x--;
  90.       Comment_Killer();
  91.       rmspace();
  92.     }
  93.   }
  94. }
  95.  
  96.  
  97. /**********************************************************
  98.  
  99.       Return value if color is in range 0<=cnum<=CNUM
  100.       otherwise call error routine.
  101.  
  102.  **********************************************************/
  103.  
  104. float InRange(cnum)
  105.   float cnum;
  106. {
  107.   if (cnum>=0 && cnum<=1.00) return(cnum);
  108.   Error(COLOR_VALUE_ERR,1501);
  109. }
  110.  
  111.  
  112. /**********************************************************
  113.  
  114.          Return value if value is >=0.
  115.          otherwise call error routine.
  116.  
  117.  **********************************************************/
  118.  
  119. float IsPos(val)
  120.   float val;
  121. {
  122.   if (val >= 0) return(val);
  123.   Error(LESS_THAN_ZERO,1502);
  124. }
  125.  
  126. /**********************************************************
  127.  
  128.     Reads next number and converts to float from string
  129.  
  130.  **********************************************************/
  131.  
  132. float Get_Next_Num() {
  133.   char str[SLEN];
  134.   float val;
  135.  
  136.   GetToken(str);
  137.  
  138.   val=atof(str);
  139.  
  140. # ifdef IODEBUG
  141.     printf("GETNEXTNUM: token=%s, val=%f\n",str,val);
  142. # endif
  143.  
  144.   return(val);
  145. }
  146.  
  147.  
  148. /**********************************************************
  149.  
  150.       Reads a name from input, and allocates some space
  151.       for it. Returns a pointer to space.
  152.  
  153.  **********************************************************/
  154.  
  155. char *Get_Next_Name() {
  156.   char str[SLEN], *s;
  157.  
  158.   GetToken(str);
  159.  
  160.   if ((s=malloc(strlen(str)))==NULL)
  161.     Error(MALLOC_FAILURE,1503);
  162.  
  163.   strcpy(s,str);
  164.  
  165. # ifdef IODEBUG
  166.     printf("GETNEXTNAME: token=%s\n",str);
  167. # endif
  168.  
  169.   return(s);
  170. }
  171.  
  172. /**********************************************************
  173.  
  174.      Reads a number 0..1 and returns a color value
  175.      0..CNUM;
  176.  
  177.  **********************************************************/
  178.  
  179. short Get_Color_Val() {
  180.   return((short)(InRange(Get_Next_Num())*(float)CNUM));
  181. }
  182.  
  183.  
  184. /**********************************************************
  185.  
  186.        Returns true if the next token is a left paren
  187.  
  188.  **********************************************************/
  189.  
  190. GetLeftParen() {
  191.   char str[SLEN];
  192.  
  193.   GetToken(str);
  194.   if (strcmp(str,"(")!=0) Error(LPAREN_EXPECTED,1504);
  195.   return;
  196. }
  197.  
  198.  
  199. /**********************************************************
  200.  
  201.        Returns true if the next token is a left paren
  202.  
  203.  **********************************************************/
  204.  
  205. int GetRightParen() {
  206.   char str[SLEN];
  207.  
  208.   GetToken(str);
  209.   if (strcmp(str,")")!=0) return(FALSE);
  210.   return(TRUE);
  211. }
  212.  
  213.  
  214. /**********************************************************
  215.  
  216.            Gets a VECTOR structure of the form
  217.            (num1, num2, num3)
  218.  
  219.  **********************************************************/
  220.  
  221. GetVector(vector)
  222.   VECT_PTR vector;
  223. {
  224.   GetLeftParen();
  225.  
  226.   vector->x = Get_Next_Num();
  227.   vector->y = Get_Next_Num();
  228.   vector->z = Get_Next_Num();
  229.  
  230.   if(!GetRightParen()) Error(ILLEGAL_VECTOR,1505);
  231. }
  232.  
  233.  
  234. /**********************************************************
  235.  
  236.            Gets a SVECTOR structure of the form
  237.            (num1, num2, num3)
  238.  
  239.  **********************************************************/
  240.  
  241. GetSVector(svector)
  242.   SVECT_PTR svector;
  243. {
  244.   GetLeftParen();
  245.  
  246.   svector->r = Get_Color_Val();
  247.   svector->g = Get_Color_Val();
  248.   svector->b = Get_Color_Val();
  249.  
  250.   if(!GetRightParen()) Error(ILLEGAL_SVECTOR,1506);
  251. }
  252.  
  253.  
  254.